package qsoft.omanga.common.utils;

import android.content.Context;

import java.io.File;

/**
 * 
 * @author sontt
 * 
 */
public class FileUtil {
	/**
	 * 
	 */
    public static String path = "";
    /**
     * 
     */
    public static final byte[] FORMAT_PNG = { (byte) 0x89, (byte) 0x50,
            (byte) 0x4E, (byte) 0x47, (byte) 0x0D, (byte) 0x0A, (byte) 0x1A,
            (byte) 0x0A };
    /**
     * 
     */
    public static final byte[] FORMAT_JPG = { (byte) 0xFF, (byte) 0xD8 };
    /**
     * 
     */
    public static final int SIZE_IMAGE = 50 * 1024;
    /**
     * 
     */
    public static final byte[] BYTE_BUFFER = new byte[512];
    /**
     * 
     */
    public static final int HEADER_FILE = 8;
    /**
     * 
     */
    public static final String EXTENSION_PNG = ".png";
    /**
     * 
     */
    public static final String EXTENSION_JPG = ".jpg";
    /**
     * 
     */
    public static final String IMAGE_LARGE = "large";
    /**
     * 
     */
    public static final String IMAGE_SMALL = "small";
    /**
     * 
     */
    public static final String MANGA = "manga";
    /**
     * 
     */
    public static final String CHAPTER = "chapter";
    /**
     * 
     */
    public static final String PAGE = "page";

    /**
     * @author sontt
     * @param context
     *            Context
     * @return String path for cache
     */
    public static String getPathCache(Context context) {
        File cacheDir = context.getFilesDir();
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }
        path = cacheDir.getPath();
        return cacheDir.getPath();
    }

    /**
     * @author sontt
     * @param mangaId
     *            String
     * @param sizeImage
     *            String
     * @return String image name of manga
     */
    public static String getImageName(String mangaId, String sizeImage) {
        return MANGA + mangaId + "_" + sizeImage;
    }

    /**
     * @author sontt
     * @param mangaId
     *            String
     * @param chapterId
     *            String
     * @param pageId
     *            String
     * @return String name content image of chapter
     */
    public static String getImageName(String mangaId, String chapterId,
            String pageId) {
        return MANGA + mangaId + "_" + CHAPTER + chapterId + "_" + PAGE
                + pageId;
    }

    public static String getNameWithoutExtension(String dirPath, String fileName) {
        return FileUtil.path + File.separator + fileName;
    }

    public static boolean isExist(String fullPath) {
        File f = new File(fullPath);
        return f.exists();
    }

    public static boolean isExistWithoutExtension(String dirPath,
            String fileName) {
        File dir = new File(dirPath);
        File[] listFile = dir.listFiles();
        boolean flag = false;
        for (int i = 0; i < listFile.length; i++) {
            String name = listFile[i].getName();
            String nameWithoutExtension = name.substring(0,
                    name.lastIndexOf('.'));
            if (nameWithoutExtension.equals(fileName)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    public static void deleteFileWithoutExtension(String fullName) {
        String dirPath = fullName.substring(0,
                fullName.lastIndexOf(File.separator));
        String fileName = fullName.substring(
                fullName.lastIndexOf(File.separator) + 1, fullName.length());
        File dir = new File(dirPath);
        File[] listFile = dir.listFiles();
        for (int i = 0; i < listFile.length; i++) {
            String name = listFile[i].getName();
            String nameWithoutExtension = name.substring(0,
                    name.lastIndexOf('.'));
            if (nameWithoutExtension.equals(fileName)) {
                listFile[i].delete();
            }
        }
    }

    public static void deleteFileZero(String dirPath) {
        File dir = new File(dirPath);
        File[] listFile = dir.listFiles();
        for (int i = 0; i < listFile.length; i++) {
            if (listFile[i].length() == 0) {
                listFile[i].delete();
            }
        }
    }
}
